package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.bean.NewPostBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间 2017 年 09 月 08.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.adapter
 * 工程名 welbeing_pregnant
 */
public class NewPostAdapter extends  RecyclerView.Adapter<NewPostAdapter.ViewHolder> implements View.OnClickListener{
    private static final String TAG = "NewPostAdapter";
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private  List<NewPostBean.DataBean> mlist = new ArrayList<>();
    private Context content;
    public NewPostAdapter(Context mcontent) {
        this.content = mcontent;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_post_recyclerview,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(content).load(RBConstants.SERVER_URL+"/Public/Uploads/"+mlist.get(position).cus_logo).transform(new GlideCircleTransform(content)).into(holder.iv_personal_head);
        holder.tv_name.setText(mlist.get(position).cus_name);
        holder.tv_date.setText(mlist.get(position).addtime);
        int state =  mlist.get(position).state;
        if(state == 0){  //不关注
            holder.tv_attention.setText("关注");
            holder.tv_attention.setTextColor(0xffFFB3B3);
        }else if(state == 1){  //已经关注
            holder.tv_attention.setText("已关注");
            holder.tv_attention.setTextColor(0xffCECECE);
        }
//        String po_url = mlist.get(position).po_url;
//        if(po_url.equals("")|| po_url == null){
//            holder.ll_user_picture.setVisibility(View.GONE);
//        }else{
//            String[] split = po_url.split(",");
//            if(split.length == 1){
//                Glide.with(content) .load(RBConstants.SERVER_URL+"/Public/Uploads/"+split[0]).into(holder.iv_user_picture_01);
//                holder.iv_user_picture_02.setVisibility(View.GONE);
//                holder.iv_user_picture_03.setVisibility(View.GONE);
//            }else if(split.length == 2){
//                Glide.with(content) .load(RBConstants.SERVER_URL+"/Public/Uploads/"+split[0]).into(holder.iv_user_picture_01);
//                Glide.with(content) .load(RBConstants.SERVER_URL+"/Public/Uploads/"+split[1]).into(holder.iv_user_picture_02);
//                holder.iv_user_picture_03.setVisibility(View.INVISIBLE);
//            }else{
//                Glide.with(content) .load(RBConstants.SERVER_URL+"/Public/Uploads/"+split[0]).into(holder.iv_user_picture_01);
//                Glide.with(content) .load(RBConstants.SERVER_URL+"/Public/Uploads/"+split[1]).into(holder.iv_user_picture_02);
//                Glide.with(content) .load(RBConstants.SERVER_URL+"/Public/Uploads/"+split[2]).into(holder.iv_user_picture_03);
//            }
//        }
        int lstate =  mlist.get(position).lstate;
        if(lstate == 0){
            holder.iv_thumb_up.setImageResource(R.mipmap.zan);
        }else if(lstate == 1){
            holder.iv_thumb_up.setImageResource(R.mipmap.zan_02);
        }
        holder.tv_titile.setText(mlist.get(position).po_name);
        holder.tv_content.setText(mlist.get(position).content);
        holder.tv_thumb_up.setText(mlist.get(position).hits_like+"");
        holder.tv_commentaries.setText(mlist.get(position).count);

       // Log.e(TAG,"onBindViewHolder=========>"+po_url);


    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }
    public void addItem( List<NewPostBean.DataBean> bean) {
        Log.e(TAG,"mlist=========>"+mlist);
        Log.e(TAG,"bean=========>"+bean);
        mlist.addAll(bean);
       notifyDataSetChanged();
    }
    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(view);
        }
    }
    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_name;
        private  TextView tv_date;
        private  TextView tv_attention;

        private  TextView tv_titile;
        private  TextView tv_content;
        private  TextView tv_thumb_up;
        private  TextView tv_commentaries;
        private ImageView iv_personal_head;
        private ImageView iv_user_picture_01;
        private ImageView iv_user_picture_02;
        private ImageView iv_user_picture_03;
        private ImageView iv_thumb_up;
        private LinearLayout ll_user_picture;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_name =  (TextView) itemView.findViewById(R.id.item_news_post_recyclerview_tv_name);
            tv_date =  (TextView) itemView.findViewById(R.id.item_news_post_recyclerview_tv_date);
            tv_attention =  (TextView) itemView.findViewById(R.id.item_news_post_recyclerview_tv_attention);

            tv_titile =  (TextView) itemView.findViewById(R.id.item_news_post_recyclerview_tv_titile);
            tv_content =  (TextView) itemView.findViewById(R.id.item_news_post_recyclerview_tv_content);
            tv_thumb_up =  (TextView) itemView.findViewById(R.id.item_news_post_recyclerview_tv_thumb_up);
            tv_commentaries =  (TextView) itemView.findViewById(R.id.item_news_post_recyclerview_tv_commentaries);
            iv_personal_head =  (ImageView) itemView.findViewById(R.id.item_news_post_recyclerview_iv_personal_head);
            iv_user_picture_01 =  (ImageView) itemView.findViewById(R.id.item_news_post_recyclerview_iv_user_picture_01);
            iv_user_picture_02 =  (ImageView) itemView.findViewById(R.id.item_news_post_recyclerview_iv_user_picture_02);
            iv_user_picture_03 =  (ImageView) itemView.findViewById(R.id.item_news_post_recyclerview_iv_user_picture_03);
            iv_thumb_up =  (ImageView) itemView.findViewById(R.id.item_news_post_recyclerview_iv_thumb_up);
            ll_user_picture =  (LinearLayout) itemView.findViewById(R.id.item_news_post_recyclerview_ll_user_picture);
        }
    }
}
